{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(891, 12)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np \n",
    "import pandas as pd  \n",
    "pd.set_option('display.max_column', 100)\n",
    "import matplotlib.pyplot as plt \n",
    "import seaborn as sns \n",
    "\n",
    "df = pd.read_csv('titanic_train.csv')\n",
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 891 entries, 0 to 890\n",
      "Data columns (total 12 columns):\n",
      "PassengerId    891 non-null int64\n",
      "Survived       891 non-null int64\n",
      "Pclass         891 non-null int64\n",
      "Name           891 non-null object\n",
      "Sex            891 non-null object\n",
      "Age            714 non-null float64\n",
      "SibSp          891 non-null int64\n",
      "Parch          891 non-null int64\n",
      "Ticket         891 non-null object\n",
      "Fare           891 non-null float64\n",
      "Cabin          204 non-null object\n",
      "Embarked       889 non-null object\n",
      "dtypes: float64(2), int64(5), object(5)\n",
      "memory usage: 83.6+ KB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund, Mr. Owen Harris</td>\n",
       "      <td>male</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
       "      <td>female</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>Heikkinen, Miss. Laina</td>\n",
       "      <td>female</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   PassengerId  Survived  Pclass  \\\n",
       "0            1         0       3   \n",
       "1            2         1       1   \n",
       "2            3         1       3   \n",
       "\n",
       "                                                Name     Sex   Age  SibSp  \\\n",
       "0                            Braund, Mr. Owen Harris    male  22.0      1   \n",
       "1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1   \n",
       "2                             Heikkinen, Miss. Laina  female  26.0      0   \n",
       "\n",
       "   Parch            Ticket     Fare Cabin Embarked  \n",
       "0      0         A/5 21171   7.2500   NaN        S  \n",
       "1      0          PC 17599  71.2833   C85        C  \n",
       "2      0  STON/O2. 3101282   7.9250   NaN        S  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df.drop_duplicates()\n",
    "df['Sex'] = (df['Sex']=='male').astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['Age_missing'] = df['Age'].isnull().astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Logistic: [0.79124579 0.81818182 0.78114478]\n",
      "random forest: [0.76094276 0.81818182 0.80808081]\n"
     ]
    }
   ],
   "source": [
    "from sklearn import model_selection \n",
    "from sklearn.linear_model import LogisticRegression \n",
    "from sklearn.ensemble import RandomForestClassifier \n",
    "\n",
    "predictors = ['Pclass', 'Sex', 'Age_missing', 'SibSp', 'Parch']\n",
    "lgr = LogisticRegression(random_state=1)\n",
    "lgr.fit(df[predictors], df['Survived'])\n",
    "scores = model_selection.cross_val_score(lgr, df[predictors], df['Survived'])\n",
    "print('Logistic:', scores)\n",
    "\n",
    "alg = RandomForestClassifier(random_state=1, n_estimators=100, min_samples_split=4, min_samples_leaf=4)\n",
    "scores = model_selection.cross_val_score(alg, df[predictors], df['Survived'])\n",
    "print('random forest:', scores)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ['Pclass', 'Sex']\n",
    "Logistic: [0.78787879 0.8013468  0.77104377]\n",
    "random forest: [0.75757576 0.8013468  0.77104377]\n",
    "\n",
    "['Pclass', 'Sex', 'Age_missing']\n",
    "Logistic: [0.76767677 0.8013468  0.77104377]\n",
    "random forest: [0.77777778 0.81818182 0.79124579]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 正则匹配需要的字符\n",
    "df['Cabin'] = df.Cabin.str.extract('([A-Z])', expand=False)\n",
    "df['Cabin'].fillna('Missing', inplace=True)\n",
    "df['Age_missing'] = df.Age.isnull().astype(int)\n",
    "df = df.drop('Age_group', axis=1)\n",
    "df['Age_group'] = pd.cut(df['Age'], [-2, 0,12,50,80])\n",
    "df.Age_group.unique()\n",
    "df.Age.fillna(-1, inplace=True)\n",
    "df = df.drop('Embarked', axis=1)\n",
    "df = df.drop('PassengerId', axis=1)\n",
    "df = df.drop('Ticket', axis=1)\n",
    "df = df.drop('Name', axis=1)\n",
    "df['Fare'] = df.Fare.apply(np.round)\n",
    "df['Fare_group'] = pd.cut(df['Fare'], [-1, 12, 32, 600])\n",
    "# df.groupby('Age')['Survived'].mean()\n",
    "# plt.figure(figsize=(10,20))\n",
    "# sns.countplot(y='Fare', data=df)\n",
    "df = pd.get_dummies(df, columns=['Sex', 'Cabin', 'Fare_group', 'Age_group'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['Age'] = df['Age'].fillna(df['Age'].mean())\n",
    "df['Cabin'] = df['Cabin'].str.extract('([A-Z])')\n",
    "df['Cabin'].fillna('missing', inplace=True)\n",
    "df = df.drop('PassengerId', axis=1)\n",
    "df = df.drop(['Ticket', 'Embarked'], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['Sex'] = (df['Sex'] == 'female').astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.get_dummies(df, columns=['Cabin'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['Age_group'] = pd.cut(df.Age, [-1,12,50,80])\n",
    "df = pd.get_dummies(df, columns=['Age_group'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(-1, 12], (32, 600], (12, 32]]\n",
       "Categories (3, interval[int64]): [(-1, 12] < (12, 32] < (32, 600]]"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby('Pclass')['Fare'].mean()\n",
    "df['Fare_group'] = pd.cut(df.Fare, [-1, 12, 32, 600])\n",
    "df.Fare_group.unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.get_dummies(df, columns=['Fare_group'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 891 entries, 0 to 890\n",
      "Data columns (total 26 columns):\n",
      "Survived                891 non-null int64\n",
      "Pclass                  891 non-null int64\n",
      "Name                    891 non-null object\n",
      "Sex                     891 non-null int32\n",
      "Age                     891 non-null float64\n",
      "SibSp                   891 non-null int64\n",
      "Parch                   891 non-null int64\n",
      "Fare                    891 non-null float64\n",
      "Cabin_A                 891 non-null uint8\n",
      "Cabin_B                 891 non-null uint8\n",
      "Cabin_C                 891 non-null uint8\n",
      "Cabin_D                 891 non-null uint8\n",
      "Cabin_E                 891 non-null uint8\n",
      "Cabin_F                 891 non-null uint8\n",
      "Cabin_G                 891 non-null uint8\n",
      "Cabin_T                 891 non-null uint8\n",
      "Cabin_missing           891 non-null uint8\n",
      "Age_group_(-1, 12]      891 non-null uint8\n",
      "Age_group_(12, 50]      891 non-null uint8\n",
      "Age_group_(50, 80]      891 non-null uint8\n",
      "Fare_group_(-1, 12]     891 non-null uint8\n",
      "Fare_group_(12, 32]     891 non-null uint8\n",
      "Fare_group_(32, 600]    891 non-null uint8\n",
      "Fare_group_(-1, 12]     891 non-null uint8\n",
      "Fare_group_(12, 32]     891 non-null uint8\n",
      "Fare_group_(32, 600]    891 non-null uint8\n",
      "dtypes: float64(2), int32(1), int64(4), object(1), uint8(18)\n",
      "memory usage: 114.8+ KB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin_A</th>\n",
       "      <th>Cabin_B</th>\n",
       "      <th>Cabin_C</th>\n",
       "      <th>Cabin_D</th>\n",
       "      <th>Cabin_E</th>\n",
       "      <th>Cabin_F</th>\n",
       "      <th>Cabin_G</th>\n",
       "      <th>Cabin_T</th>\n",
       "      <th>Cabin_missing</th>\n",
       "      <th>Age_group_(-1, 12]</th>\n",
       "      <th>Age_group_(12, 50]</th>\n",
       "      <th>Age_group_(50, 80]</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund, Mr. Owen Harris</td>\n",
       "      <td>0</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
       "      <td>1</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>Heikkinen, Miss. Laina</td>\n",
       "      <td>1</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Survived  Pclass                                               Name  Sex  \\\n",
       "0         0       3                            Braund, Mr. Owen Harris    0   \n",
       "1         1       1  Cumings, Mrs. John Bradley (Florence Briggs Th...    1   \n",
       "2         1       3                             Heikkinen, Miss. Laina    1   \n",
       "\n",
       "    Age  SibSp  Parch     Fare  Cabin_A  Cabin_B  Cabin_C  Cabin_D  Cabin_E  \\\n",
       "0  22.0      1      0   7.2500        0        0        0        0        0   \n",
       "1  38.0      1      0  71.2833        0        0        1        0        0   \n",
       "2  26.0      0      0   7.9250        0        0        0        0        0   \n",
       "\n",
       "   Cabin_F  Cabin_G  Cabin_T  Cabin_missing  Age_group_(-1, 12]  \\\n",
       "0        0        0        0              1                   0   \n",
       "1        0        0        0              0                   0   \n",
       "2        0        0        0              1                   0   \n",
       "\n",
       "   Age_group_(12, 50]  Age_group_(50, 80]  \n",
       "0                   1                   0  \n",
       "1                   1                   0  \n",
       "2                   1                   0  "
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Logistic: [0.77777778 0.79124579 0.7979798 ]\n",
      "random forest: [0.76430976 0.82828283 0.82491582]\n"
     ]
    }
   ],
   "source": [
    "from sklearn import model_selection \n",
    "from sklearn.linear_model import LogisticRegression \n",
    "from sklearn.ensemble import RandomForestClassifier \n",
    "\n",
    "predictors = ['Pclass', 'Sex', 'SibSp', 'Parch',\n",
    "             'Cabin_A','Cabin_B','Cabin_C','Cabin_D','Cabin_E',\n",
    "              'Cabin_F','Cabin_G','Cabin_T','Cabin_missing',\n",
    "             'Age_group_(-1, 12]', 'Age_group_(12, 50]','Age_group_(50, 80]',\n",
    "            'Fare_group_(-1, 12]', 'Fare_group_(12, 32]','Fare_group_(32, 600]']\n",
    "lgr = LogisticRegression(random_state=1)\n",
    "lgr.fit(df[predictors], df['Survived'])\n",
    "scores = model_selection.cross_val_score(lgr, df[predictors], df['Survived'])\n",
    "print('Logistic:', scores)\n",
    "\n",
    "alg = RandomForestClassifier(random_state=1, n_estimators=10, \n",
    "                             min_samples_split=2, min_samples_leaf=2)\n",
    "scores = model_selection.cross_val_score(alg, df[predictors], df['Survived'])\n",
    "print('random forest:', scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_test = pd.read_csv()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
